package sk.stuba.fiit.clientApp.service.bussiness;

import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import sk.stuba.fiit.clientApp.cvo.BasicPanelDataCVO;
import sk.stuba.fiit.clientApp.service.CryptographyService;
import sk.stuba.fiit.clientApp.service.StorageService;
import sk.stuba.fiit.clientApp.service.impl.CryptographyServiceImpl;
import sk.stuba.fiit.clientApp.service.impl.StorageServiceImpl;
import sk.stuba.fiit.clientApp.util.Util;


public class SignedInfoBussinesDataObject {

	@Autowired
	private StorageService storageService = new StorageServiceImpl(); 
	@Autowired
	private CryptographyService cryptographyService = new CryptographyServiceImpl();
	
	private String signAlgorithm; 
	private String canonicalizationMethod;
	private List<ReferenceBussinessData>reference;

	public String getSignAlgorithm() {
		return signAlgorithm;
	}

	public void setSignAlgorithm(String signAlgorithm) {
		this.signAlgorithm = signAlgorithm;
	}

	public String getCanonicalizationMethod() {
		return canonicalizationMethod;
	}

	public void setCanonicalizationMethod(String canonicalizationMethod) {
		this.canonicalizationMethod = canonicalizationMethod;
	}

	public List<ReferenceBussinessData> getReference() {
		return reference;
	}

	public void setReference(BasicPanelDataCVO data) throws NoSuchAlgorithmException, IOException {
		List<ReferenceBussinessData> reference = new ArrayList<ReferenceBussinessData>(data.getFiles().size()+2);
		for (File file : data.getFiles()) {
			ReferenceBussinessData cvo = new ReferenceBussinessData();
			cvo.setDigestMethod(Util.XMLDSIG_PREFIX+data.getDigestMethod());
			cvo.setUrn(file.getName());		
			cvo.setDigestValue(cryptographyService.calculateHash(storageService.readFile(file), data.getDigestMethod()));
			reference.add(cvo);
		}
		this.reference = reference;
	}
}
